package dao.impl;

import dao.AccountDao;
import entity.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
   //id
    @Override
    public Account findAccountByID(Integer ID) {
        List<Account>  accounts =super.getJdbcTemplate().query("select * from account where id=?",
                new BeanPropertyRowMapper<Account>(Account.class),ID);

        return accounts.isEmpty()?null : accounts.get(0);
    }



    //名称
    @Override
    public Account findAccountByName(String name) {
        List<Account>  accounts =super.getJdbcTemplate().query("select * from account where name=?",
                new BeanPropertyRowMapper<Account>(Account.class),name);
        if(accounts.isEmpty()){
            return null;
        }
        if(accounts.size()>1){
            throw  new RuntimeException("多个账户名称相同");
        }
        return accounts.isEmpty()?null : accounts.get(0);
    }
//跟新
    @Override
    public void updateAccount(Account account) {
        super.getJdbcTemplate().update("update  account set name=?,money=? where id=?"
                ,account.getName(),account.getMoney(),account.getId());

    }
    //id

}
